package _021_10_5

import "math"

//暴力法超时
func maxLength(arr []int) int {
	childArr := make([][]int, 10)
	maxLen := 0
	db := 0
	//data:=make(map[[]int]int)
	for i := 0; i < len(arr); i++ {
		for j := i + 1; j <= len(arr); j++ {
			db = 0
			for k := 0; k < len(arr[i:j])-1; k++ {
				if arr[i:j][k] >= arr[i:j][k+1] {
					db++
					break
				}
			}

			for k := 0; k < len(arr[i:j])-1; k++ {
				if arr[i:j][k] <= arr[i:j][k+1] {
					db++
					break
				}
			}
			if db != 2 {
				childArr = append(childArr, arr[i:j])
			}
		}
	}
	//isLineArr := true
	for _, item := range childArr {
		if maxLen < len(item) {
			maxLen = len(item)
		}
		//isLineArr = true
		//for i := 0; i < len(item)-1; i++ {
		//	if item[i] >= item[i+1] {
		//		isLineArr = false
		//		break
		//	}
		//}
		//if isLineArr {
		//	if maxLen < len(item) {
		//		maxLen = len(item)
		//	}
		//}
		//for i := 0; i < len(item)-1; i++ {
		//	if item[i] <= item[i+1] {
		//		isLineArr = false
		//		break
		//	}
		//}
		//if isLineArr {
		//	if maxLen < len(item) {
		//		maxLen = len(item)
		//	}
		//}
	}
	return maxLen
}

func maxLength2(arr []int) int {

	left, right := 0, 0
	maxLen := 0
	window := make(map[int]int)
	for right = 0; right < len(arr); right++ {
		index, ok := window[arr[right]]
		if ok {
			left = int(math.Max(float64(index+1), float64(left)))
		}
		if maxLen < right-left+1 {
			maxLen = right - left + 1
		}
		window[arr[right]] = right
	}
	return maxLen
}
